package com.itboye.textileexpres.login;

import java.util.Observable;
import java.util.Observer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.textileexpes.presenter.GetYanzhengMaPresenter;
import com.itboye.textileexpes.presenter.MainPresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.util.TimeCount;

public class ForgetPassWordActivity extends BaseActivity implements
		OnClickListener, Observer {
	EditText phone_num, new_password, pass_word;
	Button getVD;
	TextView login_tv;
	MainPresenter mainPresenter;
	String code;
	String phone;
	String password;
	TimeCount count;
	ImageView back;
	GetYanzhengMaPresenter getYanzhengMaPresenter;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_forget_password;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		mainPresenter = new MainPresenter(this);
		count = new TimeCount(60000, 1000, getVD);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.login_tv:
			phone = phone_num.getText() + "";
			code = pass_word.getText() + "";
			password = new_password.getText() + "";
			if (isEmpty(phone)) {
				toast(R.string.h1151 + "");
			}
			if (isEmpty(password)) {
				toast(R.string.h1165 + "");
			}
			if (isEmpty(code)) {
				toast(R.string.h1166 + "");
			} else {
				showProgressDialog(R.string.h99 + "", true);
				String VD = (code);
				mainPresenter
						.updatePasswordByTelephone(VD, "", password, phone);
			}
			break;
		case R.id.getVD:
			phone = phone_num.getText() + "";
			Pattern p = Pattern
					.compile("^((13[0-9])|(17\\d)|(14\\d)|(15[^4,\\D])|(18[0-9]))\\d{8}$");
			Matcher m = p.matcher(phone);
			if ("".equals(phone) || phone == "") {
				toast(R.string.h1151 + "");
				return;
			} else if (m.matches()) {
				getYanzhengMaPresenter = new GetYanzhengMaPresenter(this);
				getYanzhengMaPresenter.getVerificationCode("", phone, 2 + "");
				count.start();
			} else {
				toast(R.string.h1167 + "");
			}
			break;
		default:
			break;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = super.handlerError(data);
		if (entity != null) {
			if (entity.getEventType().equals(
					GetYanzhengMaPresenter.YanzhengmaSuccessEvent)) {
				code = entity.getData().toString();
				String VD = code
						.substring(code.length() - 8, code.length() - 2);
				pass_word.setText(VD);
				toast(code);
			} else if (entity.getEventType().equals(
					GetYanzhengMaPresenter.YanzhengmaFailEvent)) {
				toast(R.string.h1168 + "");
			}
			if (entity.getEventType().equals(
					MainPresenter.UpdatePwdByPhoneSuccessEvent)) {
				toast(R.string.h1169 + "");
				try {
					closeProgressDialog();
				} catch (Exception e) {
					// TODO: handle exception
				}
				finish();
			} else if (entity.getEventType().equals(
					MainPresenter.UpdatePwdByPhoneFailureEvent)) {
				toast(entity.getData().toString());

			}
		}
		try {
			closeProgressDialog();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public boolean isEmpty(String msg) {
		if (msg == null || msg.equals(""))
			return true;
		return false;
	}
}
